package logic

import (
	"context"
	"cp/cache"
	"cp/cprpc/cprpc"
	"cp/cprpc/internal/svc"
	"cp/models/mysql"
	"github.com/zeromicro/go-zero/core/logx"
	"time"
)

type CourseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCourseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CourseListLogic {
	return &CourseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CourseListLogic) CourseList(in *cprpc.CourseListRequest) (*cprpc.CourseListResponse, error) {
	// todo: add your logic here and delete this line

	type course []*mysql.Course
	mysql.DB.Debug().Table("courses").
		Select("LEFT JOIN  teachers ON teachers.id=courses.teacher_id").
		Find(&course{})

	cache.Rdb.Set(l.ctx, "course", course{}, time.Minute*3)

	return &cprpc.CourseListResponse{}, nil
}
